package demo.hdz.mediacodecdecode.fragment;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import demo.hdz.mediacodecdecode.R;
import demo.hdz.mediacodecdecode.customview.ImageGLSurfaceView;


public class AdjustImageFragment extends Fragment {

    private ImageGLSurfaceView glSurfaceView;

    public AdjustImageFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_adjust_image, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        glSurfaceView = view.findViewById(R.id.imageGLSurfaceView);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.raw.carplay);
        glSurfaceView.setBitmap(bitmap);

        initSeekBar(view);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }



    private void initSeekBar(View view) {
        SeekBar saturationSeekBar = view.findViewById(R.id.saturationSeekBar);
        saturationSeekBar.setMax(100);
        saturationSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (glSurfaceView != null) {
                    glSurfaceView.setParameter("saturation", progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        saturationSeekBar.setProgress(50);


        SeekBar brightnessSeekBar = view.findViewById(R.id.brightnessSeekBar);
        brightnessSeekBar.setMax(20);
        brightnessSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (glSurfaceView != null) {
                    glSurfaceView.setParameter("brightness", progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        brightnessSeekBar.setProgress(10);


        SeekBar contrastSeekBar = view.findViewById(R.id.contrastSeekBar);
        contrastSeekBar.setMax(100);
        contrastSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (glSurfaceView != null) {
                    glSurfaceView.setParameter("contrast", progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        contrastSeekBar.setProgress(50);


        SeekBar exposureSeekBar = view.findViewById(R.id.exposureSeekBar);
        exposureSeekBar.setMax(100);
        exposureSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (glSurfaceView != null) {
                    glSurfaceView.setParameter("exposure", progress);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        exposureSeekBar.setProgress(50);
    }
}